﻿using Utilities.Log;
using Wx.Model.Qr;
using Wx.Core;
using System;

namespace Wx.Api
{
    public static class Qr
    {
        private static LogHelper log = new LogHelper(typeof(Message));

        /// <summary>
        /// 创建二维码，获取Ticket
        /// </summary>
        /// <param name="expire_seconds">该二维码有效时间，以秒为单位。 最大不超过2592000（即30天），此字段如果不填，则默认有效期为30秒</param>
        /// <param name="action_name">二维码类型，QR_SCENE为临时,QR_LIMIT_SCENE为永久,QR_LIMIT_STR_SCENE为永久的字符串参数值</param>
        /// <param name="scene_id">QR_LIMIT_SCENE必选参，场景值ID，临时二维码时为32位非0整型，永久二维码时最大值为100000（目前参数只支持1--100000）</param>
        /// <param name="scene_str">QR_LIMIT_STR_SCENE必选参，场景值ID（字符串形式的ID），字符串类型，长度限制为1到64，仅永久二维码支持此字段</param>
        /// <returns></returns>
        public static Ticket GetTicket(int expire_seconds, Model.Qr.Action.Name action_name, int scene_id = 0, string scene_str = null)
        {
            try
            {
                string url = string.Format(Config.GetApiUrl("QrTicket"), Token.AccessToken);
                string postData = string.Empty;
                switch (action_name)
                {
                    case Model.Qr.Action.Name.QR_SCENE:
                        {
                            postData = "{\"expire_seconds\": " + expire_seconds + ", \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": " + scene_id + "}}}";
                            break;
                        }
                    case Model.Qr.Action.Name.QR_LIMIT_SCENE:
                        {
                            postData = "{\"expire_seconds\": " + expire_seconds + ", \"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": " + scene_id + "}}}";
                            break;
                        }
                    case Model.Qr.Action.Name.QR_LIMIT_STR_SCENE:
                        {
                            postData = "{\"expire_seconds\": " + expire_seconds + ", \"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": " + scene_id + "}}}";
                            break;
                        }
                    default:
                        {
                            postData = "{\"expire_seconds\": " + expire_seconds + ", \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": " + scene_id + "}}}";
                            break;
                        }
                }
                Model.HttpResult result = Core.HttpUtility.Post(url, postData);
                if (result.status)
                {
                    log.info("Get Qr Ticket:" + result.msg);
                    return Newtonsoft.Json.JsonConvert.DeserializeObject<Model.Qr.Ticket>(result.msg);
                }
                else
                {
                    log.info("Get Qr Ticket Error" + result.msg);
                    return default(Ticket);
                }
            }
            catch (Exception e)
            {
                log.error(e);
            }
            return default(Ticket);
        }

        /// <summary>
        /// 下载二维码
        /// </summary>
        /// <param name="ticket">票据</param>
        /// <param name="filePath">存储地址</param>
        public static void GetQr(string ticket, string filePath)
        {
            string url = string.Format(Config.GetApiUrl("GetQr"), System.Web.HttpUtility.UrlEncode(ticket, System.Text.Encoding.UTF8));
            HttpUtility.DownloadFile(url, filePath);
        }
    }
}
